#include "thread.hpp"
#include <iostream>
#include <sstream>
#include <chrono>
#include <thread>
#include <mutex>

std::mutex print_mtx;

void safe_print(const std::string& msg) {
    std::lock_guard<std::mutex> lock(print_mtx);
    std::cout << msg << std::endl;
}

int main() {
    ThreadPool pool(4);

    for (int i = 0; i < 10; ++i) {
        pool.enqueue([i]() {
            std::ostringstream oss;
            oss << "[Task " << i << "] Start in thread " << std::this_thread::get_id();
            safe_print(oss.str());

            std::this_thread::sleep_for(std::chrono::milliseconds(100));  // 模拟工作

            std::ostringstream done;
            done << "[Task " << i << "] Done";
            safe_print(done.str());
        });
    }

    std::this_thread::sleep_for(std::chrono::seconds(2));  // 等待所有任务执行完
    safe_print("[Main] All tasks submitted.");
    return 0;
}

